package cn.jn.lhm.teacher.handler;

import cn.jn.lhm.teacher.entity.EduTeacher;
import cn.jn.lhm.teacher.entity.vo.TeacherQueryCondition;
import cn.jn.lhm.teacher.service.EduTeacherService;
import cn.jn.lhm.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

/**
 * @author LHM
 * @Tips 热爱每一天，奋斗每一天
 * @date 2020/5/27 - 21:44
 */
@Slf4j
@RestController
@RequestMapping("/teacher")
public class EduServiceHandler {

    @Resource
    private EduTeacherService eduTeacherService;


    @GetMapping("/getAll")
    public R getAll(){
        return R.ok().data("data", eduTeacherService.listByWrap(null));
    }

    @GetMapping("/page/{current}/{limit}")
    public R pageList(@PathVariable("current")Long current,
                      @PathVariable("limit")Long limit){
        Page<EduTeacher> page = new Page<>(current, limit);
        IPage<EduTeacher> iPage = eduTeacherService.listByPage(page, null);
        return R.ok().data("data", iPage.getRecords()).data("total", iPage.getTotal());
    }


    @GetMapping("/get/{id}")
    public R getTeacherById(@PathVariable("id")Long id){
        return R.ok().data("data", eduTeacherService.getById(id));
    }


    @DeleteMapping("/delete/{id}")
    public R deleteById(@PathVariable("id")Long id){
        if(eduTeacherService.deleteById(id)){
            return R.ok();
        }else {
            return R.error();
        }
    }


    @PostMapping("/pageCondition/{current}/{limit}")
    public R pageListCondition(@PathVariable("current")Long current,
                               @PathVariable("limit")Long limit,
                               @RequestBody(required = false) TeacherQueryCondition condition){
        //创建查询对象
        log.info("接收到的查询条件为：" + condition);
        Page<EduTeacher> page = new Page<>(current, limit);
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        //wrapper
        if(!StringUtils.isEmpty(condition.getName())){
            wrapper.like("name", condition.getName());
        }
        if (condition.getLevel() != null){
            wrapper.eq("level",condition.getLevel());
        }
        if (!StringUtils.isEmpty(condition.getBegin())){
            wrapper.ge("gmt_create", condition.getBegin());
        }
        if (!StringUtils.isEmpty(condition.getEnd())){
            wrapper.le("gmt_modify",condition.getEnd());
        }
        //排序，按照时间来进行排序，方便在前端的第一页显示
        wrapper.orderByDesc("gmt_create");
        IPage<EduTeacher> iPage = eduTeacherService.listByPage(page, wrapper);
        return R.ok().data("data", iPage.getRecords()).data("total", iPage.getTotal());
    }

    @PostMapping("/add")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){
        return eduTeacherService.save(eduTeacher) ? R.ok() : R.error();
    }

    @PostMapping("/update/{id}")
    public R updateTeacher(@PathVariable("id")Long id,
                           @RequestBody EduTeacher eduTeacher){
        return eduTeacherService.updateById(eduTeacher)? R.ok() : R.error();
    }

}
